package com.example.sxt_practise.money;

import java.util.Scanner;

/**
 * 将数字金额转为大写汉字金额
 * @author gaofeng
 * @date 2019年6月18日 下午10:52:27
 */
public class RMB {
    /**
     *  将数字金额转为大写汉字金额
     */
    public static class ConvertUpMoney {

        //大写数字
        private static final String[] NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        // 整数部分的单位
        private static final String[] IUNIT = {"圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟"};
        //小数部分的单位
        private static final String[] DUNIT = {"角", "分", "厘"};

        private static final String CN_ZERO = "零角零分";

        private static final String CN_DECIMAL = "零分";

        private static final String CN_INT = "整";

        // 转成中文的大写金额
        public static String toChinese(String str) {
            if ("0".equals(str) || "0.00".equals(str) || "0.0".equals(str)) {
                return "零元";
            }
            // 判断是否存在负号"-"
            boolean flag = false;
            if (str.startsWith("-")) {
                flag = true;
                str = str.replaceAll("-", "");
            }
            // 去掉","
            str = str.replaceAll(",", "");
            // 整数部分数字
            String integerStr;
            // 小数部分数字
            String decimalStr;


            // 初始化：分离整数部分和小数部分
            if (str.indexOf(".") > 0) {
                integerStr = str.substring(0, str.indexOf("."));
                decimalStr = str.substring(str.indexOf(".") + 1);
            } else if (str.indexOf(".") == 0) {
                integerStr = "";
                decimalStr = str.substring(1);
            } else {
                integerStr = str;
                decimalStr = "";
            }

            //beyond超出计算能力，直接返回
            if (integerStr.length() > IUNIT.length) {
                System.out.println(str + "：超出计算能力");
                return str;
            }
            //整数部分数字
            int[] integers = toIntArray(integerStr);
            //判断整数部分是否存在输入012的情况
            if (integers.length > 1 && integers[0] == 0) {
                System.out.println("抱歉，请输入数字！");
                if (flag) {
                    str = "-" + str;
                }
                return str;
            }
            //设置万单位
            boolean isWan = isWan5(integerStr);
            //小数部分数字
            int[] decimals = toIntArray(decimalStr);
            //返回最终的大写金额
            String result = getChineseInteger(integers, isWan) + getChineseDecimal(decimals);

            if (flag) {
                //如果是负数，加上"负"
                result = "负" + result;
            } else {
                result = result;
            }
            StringBuilder temp = new StringBuilder(result);

            // 处理零角零分、零分,换成整
            Integer index = temp.indexOf(CN_DECIMAL);
            Integer index2 = temp.indexOf(CN_ZERO);

            if (index > -1) {
                temp.replace(index, index + 2, CN_INT);
            }
            if (index2 > -1) {
                temp.replace(index2, index2 + 4, CN_INT);
            }
            return temp.toString();
        }

        //将字符串转为int数组
        private static int[] toIntArray(String number) {
            int[] array = new int[number.length()];
            for (int i = 0; i < number.length(); i++) {
                array[i] = Integer.parseInt(number.substring(i, i + 1));
            }
            return array;
        }

        //将整数部分转为大写的金额
        public static String getChineseInteger(int[] integers, boolean isWan) {
            StringBuffer chineseInteger = new StringBuffer("");
            int length = integers.length;
            if (length == 1 && integers[0] == 0) {
                return "";
            }
            for (int i = 0; i < length; i++) {
                String key = "";
                if (integers[i] == 0) {
                    //万（亿）
                    if ((length - i) == 13) {
                        key = IUNIT[4];
                        //亿
                    } else if ((length - i) == 9) {
                        key = IUNIT[8];
                        //万
                    } else if ((length - i) == 5 && isWan) {
                        key = IUNIT[4];
                        //圆
                    } else if ((length - i) == 1) {
                        key = IUNIT[0];
                    }
                    if ((length - i) > 1 && integers[i + 1] != 0) {
                        key += NUMBERS[0];
                    }
                }
                chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
            }
            return chineseInteger.toString();
        }

        //将小数部分转为大写的金额
        private static String getChineseDecimal(int[] decimals) {
            StringBuffer chineseDecimal = new StringBuffer("");
            for (int i = 0; i < decimals.length; i++) {
                if (i == 3) {
                    break;
                }
                chineseDecimal.append((NUMBERS[decimals[i]] + DUNIT[i]));
            }
            return chineseDecimal.toString();
        }

        //判断当前整数部分是否已经是达到【万】
        private static boolean isWan5(String integerStr) {
            int length = integerStr.length();
            if (length > 4) {
                String subInteger = "";
                if (length > 8) {
                    subInteger = integerStr.substring(length - 8, length - 4);
                } else {
                    subInteger = integerStr.substring(0, length - 4);
                }
                return Integer.parseInt(subInteger) > 0;
            } else {
                return false;
            }
        }

        //Test
        public static void main(String[] args) {
            System.out.println("请输入数字n：");
            Scanner in = new Scanner(System.in);
            String number = in.next();
            System.out.println(number + ": " + ConvertUpMoney.toChinese(number));
        }
    }
}